#!/bin/sh

source scripts/common.sh

####################################################################################################

FT_VERSION=2.3.7
FT_BASENAME="freetype"
FT_ARCHIVE="${FT_BASENAME}-${FT_VERSION}.tar.bz2"
FT_URL="http://download.savannah.gnu.org/releases/freetype/${FT_ARCHIVE}"
FT_DIR="${FT_BASENAME}-${FT_VERSION}"
FT_PREFIX="${THIRD_PARTY_PREFIX}/${FT_BASENAME}"
FT_LIB=libfreetype.a

####################################################################################################

function build_ft()
{

target=$1

mkdir -p ${THIRD_PARTY_BUILD} || exit 1
pushd ${THIRD_PARTY_BUILD} || exit 1

report ""
report "Removing old build from '${FT_DIR}'..."
rm -fr $FT_DIR
rm -fr $FT_PREFIX/${target}

report "Preparing ${target} '${FT_BASENAME}'..."
fetch $FT_URL $FT_ARCHIVE

report "Configuring ${target} '${FT_BASENAME}'..."
report ""

pushd ${FT_DIR} || exit 1
mkdir -p ${target} || exit 1

./configure \
		CXXFLAGS="${SYSROOT} -arch ${target}" \
		CFLAGS="${SYSROOT} -arch ${target}" \
		LDFLAGS="-Wl,-syslibroot,${DEVSDK}" \
		--prefix=${FT_PREFIX}/${target} \
        --target=${target}-apple-darwin \
        --disable-shared \
        --enable-static \
        --quiet \
        --silent

if test $? -ne 0; then
        echo "configure failed"
        exit 1
fi

report "Building ${target} '${FT_BASENAME}'..."
nice make -j4 || exit 1
nice make install || exit 1

report "Built ${target} '${FT_BASENAME}'"
report ""

popd
popd

}

####################################################################################################

build_for_snow_leopard
for target in 'i686' 'x86_64' 'ppc'
do
	build_ft $target
done

create_universal_lib ${FT_PREFIX} ${FT_LIB}
create_intel_lib ${FT_PREFIX} ${FT_LIB}
create_powerpc_lib ${FT_PREFIX} ${FT_LIB}

####################################################################################################

